package com.mayflygeek.mayflyask.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mayflygeek.mayflyask.entity.User;
import com.mayflygeek.mayflyask.service.SendEmailService;
import com.mayflygeek.mayflyask.service.UserService;
import com.mayflygeek.mayflyask.util.Constants;
import com.mayflygeek.mayflyask.util.MD5Utils;
/**
 * @author 蝈蝈 
 * @email guodefu909@126.com
 * @team 蜉蝣极客
 * @link www.mayflygeek.com
 * 注册Controller
 */
@Controller
@RequestMapping(value = "/regist")
public class RegistController{
	
	private Logger log = LoggerFactory.getLogger(getClass());

	@Resource
	private UserService userService;
	
	@Resource
	private SendEmailService sendEmailService;
	
//	进入注册页
	@RequestMapping(value = "/toRegist")
	public String toRegist() {
		return "registStepOne";
	}
	
//	注册第一步，输入验证邮箱
	@RequestMapping(value = "/stepOne")
	public String registStepOne(String userName,ModelMap model) {
//		1.检测邮箱是否正确，是否可用于注册
		User userResult = userService.getByUserName(userName);
//		2.生成对应的唯一编码并保存
		String salt;
		if (userResult == null) {
			salt = MD5Utils.getRandomCharacters(8);
			User user = new User();
			user.setUserName(userName);
			user.setPassword("123456");//默认密码
			user.setNickName("nickName");//默认昵称
			user.setSalt(salt);
			user.setStatus(0);
			user.setUpdater(0L);
			userService.saveUser(user);
		}else if(userResult.getStatus() == 0){
			salt = userResult.getSalt();
		}else{
			model.addAttribute(Constants.MESSAGE,"该邮箱已经成功激活，请不要重复注册");
			return Constants.TOLOGIN;
			//提示用户已成功激活，请不要重复注册
		}
		String registUrl = "http://localhost:8080/mayflyask"+"/regist/confirm?userName="+userName+"&salt="+salt;
//		TODO Url改为配置或数据库读取
		log.info(registUrl);
//		3.发送注册邮件，提供注册URL
		sendEmailService.sendRegistEmail(userName, registUrl);
//		4.返回消息，提醒用户登录邮箱完成注册	
		model.addAttribute("userName", userName);
		return "registStepOneResult";
	}
	
//	邮箱确认，进入注册第二步
	@RequestMapping(value = "/confirm")
	public String registConfirm(String userName, String salt, ModelMap model) {
		User userResult = userService.getByUserName(userName);
		if (userResult != null && userResult.getSalt().equals(salt)) {
			model.addAttribute("userName",userName);
			return "registStepTwo";
		}
		model.addAttribute(Constants.MESSAGE,"链接无效，请重新注册！");
		return Constants.TOLOGIN;
	}
	
//	注册第二步，设置昵称，密码，并激活
	@RequestMapping(value = "/stepTwo")
	public String registStepTwo(User user,ModelMap model) {
		if (user != null && user.getUserName()!=null && user.getPassword()!=null && user.getPassword().equals(user.getConfirmPassword())) {
			User userResult = userService.getByUserName(user.getUserName());
			if (userResult !=null && userResult.getStatus()==0) {
				userResult.setNickName(user.getNickName());
				userResult.setPassword(user.getPassword());
				userResult.setStatus(1);
				userService.updateUser(userResult);
				return "registStepTwoResult";
			}
		}
		model.addAttribute(Constants.MESSAGE,"注册出错！");
//		TODO 是否需要做国际化消息？
		return Constants.TOLOGIN;
	}
	
//	查看服务条款
	@RequestMapping(value = "/toServiceTerms")
	public String toServiceTerms() {
		return "serviceTerms";
	}
}
